package com.zondy.mapgis.psmap.manager.controller;

import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.utils.poi.ExcelUtil;
import com.zondy.mapgis.common.core.web.controller.BaseController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.annotation.RequiresPermissions;
import com.zondy.mapgis.common.security.utils.SecurityUtils;
import com.zondy.mapgis.domain.psmap.AppData;
import com.zondy.mapgis.domain.psmap.AppDataTreeNode;
import com.zondy.mapgis.psmap.manager.service.IAppDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.json.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Tag(
   name = "数据目录管理",
   description = "数据目录控制器"
)
@ManagerRestController({"/app/data"})
public class AppDataController extends BaseController {
   @Resource
   private IAppDataService appDataService;

   @Operation(
      summary = "查询数据目录列表"
   )
   @PreAuthorize("@ss.hasPermi('app:data:list')")
   @RequiresPermissions({"app:data:list"})
   @GetMapping({"/list"})
   public AjaxResult list(AppData appData) {
      List<AppData> list = this.appDataService.selectAppDataList(appData);
      return this.success(list);
   }

   @Operation(
      summary = "导出数据目录列表"
   )
   @PreAuthorize("@ss.hasPermi('app:data:export')")
   @RequiresPermissions({"app:data:export"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.EXPORT
   )
   @PostMapping({"/export"})
   public void export(HttpServletResponse response, AppData appData) {
      List<AppData> list = this.appDataService.selectAppDataList(appData);
      ExcelUtil<AppData> util = new ExcelUtil<>(AppData.class);
      util.exportExcel(response, list, "数据目录数据");
   }

   @Operation(
      summary = "获取数据目录详细信息"
   )
   @PreAuthorize("@ss.hasPermi('app:data:query')")
   @RequiresPermissions({"app:data:query"})
   @GetMapping({"/{dataId}"})
   public AjaxResult getInfo(@PathVariable("dataId") Long dataId) {
      return this.success(this.appDataService.selectAppDataByDataId(dataId));
   }

   @Operation(
      summary = "新增数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:add')")
   @RequiresPermissions({"app:data:add"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.INSERT
   )
   @PostMapping
   public AjaxResult add(@RequestBody AppData appData) {
      return this.toAjax(this.appDataService.insertAppData(appData));
   }

   @Operation(
      summary = "修改数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:edit')")
   @RequiresPermissions({"app:data:edit"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.UPDATE
   )
   @PutMapping
   public AjaxResult edit(@RequestBody AppDataTreeNode appDataTreeNode) {
      return this.toAjax(this.appDataService.updateAppData(appDataTreeNode));
   }

   @Operation(
      summary = "删除数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:remove')")
   @RequiresPermissions({"app:data:remove"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.DELETE
   )
   @DeleteMapping({"/{dataIds}"})
   public AjaxResult remove(@PathVariable Long[] dataIds) {
      return this.toAjax(this.appDataService.deleteAppDataByDataIds(dataIds));
   }

   @Operation(
      summary = "更新数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:edit')")
   @RequiresPermissions({"app:data:edit"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/updateTree"})
   public AjaxResult updateTreeData(@RequestBody List<AppData> dataList) {
      return this.toAjax(this.appDataService.updateTreeData(dataList));
   }

   @Operation(
      summary = "获取树形结构数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:list')")
   @RequiresPermissions({"app:data:list"})
   @GetMapping({"/treeSelect"})
   public AjaxResult getTreeDataForManager() {
      Long userId = SecurityUtils.getUserId();
      return this.success(this.appDataService.selectAppDataTreeForManagerByUserId(userId));
   }

   @Operation(
      summary = "导入数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:import')")
   @RequiresPermissions({"app:data:import"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.IMPORT
   )
   @PostMapping({"/importData"})
   public AjaxResult importData(MultipartFile file, boolean updateSupport) {
      List<AppData> appDataList = this.appDataService.parseDatasFromFile(file);
      String operName = SecurityUtils.getUsername();
      String message = this.appDataService.importDatas(appDataList, updateSupport, operName);
      return this.success(message);
   }

   @Operation(
      summary = "导入数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:import')")
   @RequiresPermissions({"app:data:import"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.IMPORT
   )
   @PostMapping({"/importDataFromCloud"})
   public AjaxResult importDataFromCloud(@RequestBody String data) {
      JSONObject jsonObject = new JSONObject(data);
      List<AppData> appDataList = this.appDataService.parseDatasFromCloud(jsonObject.optString("data"));
      String operName = SecurityUtils.getUsername();
      String message = this.appDataService.importDatas(appDataList, jsonObject.optBoolean("updateSupport"), operName);
      return this.success(message);
   }

   @Operation(
      summary = "导出数据目录"
   )
   @PreAuthorize("@ss.hasPermi('app:data:export')")
   @RequiresPermissions({"app:data:export"})
   @Log(
      title = "数据目录",
      businessType = BusinessType.EXPORT
   )
   @PostMapping({"/exportData"})
   public void exportData(HttpServletResponse response) {
      Long userId = SecurityUtils.getUserId();
      this.appDataService.exportData(userId, response);
   }
}
